package org.easy.tool.service.impl;

import cn.hutool.core.io.FileUtil;
import org.easy.tool.common.FileIOUtil;
import org.easy.tool.common.StringUtil;
import org.easy.tool.common.TimeUtil;
import org.easy.tool.common.WebUtil;
import org.easy.tool.constant.TihaiConstant;
import org.easy.tool.entity.Problem;
import org.easy.tool.mapper.FileRecordMapper;
import org.easy.tool.mapper.ProblemMapper;
import org.easy.tool.mapper.ProblemMarkMapper;
import org.easy.tool.service.IProblem;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.easy.tool.service.IProblemMark;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 题目实现
 *
 * @author Valley
 * @since 2024-09-04 22:51:14
 */
@Service
public class ProblemService extends ServiceImpl<ProblemMapper, Problem> implements IProblem {

    @Autowired
    ProblemMarkMapper problemMarkMapper;

    @Autowired
    FileRecordMapper fileRecordMapper;

    @Override
    public boolean submitProblem(LinkedHashMap data) throws Exception {
        Problem problem = new Problem();
        String userId = WebUtil.getUserId();
        if (StringUtil.isBlank(userId)) {
            userId = "admin";
        }
        problem.setCreator(userId);
        String title = (String) data.get("title");
        problem.setTitle(title);
        String type = (String) data.get("type");
        problem.setType(type);
        Object content = data.get("content");
        if (content instanceof String) {
            problem.setContent((String) content);
        } else {
            String string = TihaiConstant.MAPPER.writeValueAsString(content);
            problem.setContent(string);
        }
        problem.setCreateTime(TimeUtil.now());
        problem.setUpdateTime(TimeUtil.now());
        save(problem);
        return true;
    }

    @Override
    public List<Problem> getProblemList() {
        return list();
    }

    @Override
    public boolean deleteProblem(LinkedHashMap data) throws Exception{
        String problemId = (String) data.get("problemId");
        //删除图片记录
        String content = getById(problemId).getContent();
        LinkedHashMap contentMap = TihaiConstant.MAPPER.readValue(content, LinkedHashMap.class);
        String contentImg = (String) contentMap.get("contentImg");
        String answerImg = (String) contentMap.get("answerImg");
        fileRecordMapper.deleteById(contentImg);
        fileRecordMapper.deleteById(answerImg);
        //删除存储的文件
        String contentPath = TihaiConstant.FILE_DIR + File.separator + contentImg;
        FileIOUtil.delete(contentPath);
        String answerPath = TihaiConstant.FILE_DIR + File.separator + answerImg;
        FileIOUtil.delete(answerPath);
        //删除题目跟标签的关系
        boolean status = problemMarkMapper.deleteByProblemId(problemId);
        return removeById(problemId) && status;
    }
}
